package bb.lanxing.adapter.discover;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;

import java.util.ArrayList;
import java.util.List;

import bb.lanxing.R;
import bb.lanxing.model.database.Bbs;

public class BbsCommentAdapter extends RecyclerView.Adapter<BbsCommentAdapter.ViewHolder> {
    private List<Bbs> list = new ArrayList<>();
    private OnItemClickListener listener;
    private LikeListener likeListener;
    private Context mContext;

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView head;
        TextView name;
        TextView content;
        ImageView image;

        TextView time;

        LinearLayout likeLayout;
        ImageView likeIcon;
        TextView like_number;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            head = itemView.findViewById(R.id.head);
            name = itemView.findViewById(R.id.name);
            content = itemView.findViewById(R.id.content);
            image = itemView.findViewById(R.id.image);

            time = itemView.findViewById(R.id.time);

            likeLayout = itemView.findViewById(R.id.bbs_like);
            likeIcon = itemView.findViewById(R.id.likeIcon);
            like_number = itemView.findViewById(R.id.like_number);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public interface LikeListener {
        void onItemClick(int position);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_bbs_comment, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (list != null) {
            Bbs bbs = list.get(position);
            if (bbs != null) {
                if (!"https://woc.bluebank.com.cn/prod-api".equals(bbs.userHead)) {
                    Glide.with(mContext).load(bbs.userHead).transform(new RoundedCorners(30)).into(holder.head);
                }
                holder.name.setText(bbs.createBy);
                holder.content.setText(bbs.content);
                if (bbs.imageList != null && bbs.imageList.size() > 0) {
                    Glide.with(mContext).load(bbs.imageList.get(0)).into(holder.image);
                } else {
                    holder.image.setVisibility(View.GONE);
                }

                holder.time.setText(bbs.createTime);

                holder.likeLayout.setOnClickListener(v -> {
                    if (likeListener != null) {
                        likeListener.onItemClick(position);
                    }
                });
                holder.like_number.setText(bbs.likesCount + "");
                if (bbs.myLike == 1) {
                    holder.likeIcon.setImageResource(R.drawable.liked);
                    holder.like_number.setTextColor(mContext.getColor(R.color.bbs_liked));
                } else {
                    holder.likeIcon.setImageResource(R.drawable.like);
                    holder.like_number.setTextColor(mContext.getColor(R.color.bbs_like));
                }
                holder.itemView.setOnClickListener(v -> {
                    if (listener != null) {
                        listener.onItemClick(position);
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public void setLikeListener(LikeListener listener) {
        likeListener = listener;
    }

    public void updateData(List<Bbs> dataList) {
        list = dataList;
        notifyDataSetChanged();
    }
}